package Admin;

import bean.Teacher;

import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.*;

import static JDBCutil.JDBCUtil.addTeacher;
/*
 * Created by JFormDesigner on Mon Aug 12 16:05:22 CST 2024
 */



/**
 * @author 29454
 */
public class AddTeacher extends JDialog {
    public AddTeacher(Window owner) {
        super(owner);
        initComponents();
        setVisible(true);
    }

    private void addteacher(ActionEvent e) {
        // TODO add your code here
        try {
            Teacher s = new Teacher();
            s.setUserid(Integer.valueOf(textField1.getText()));
            s.setUsername(textField2.getText());
            s.setSex(textField3.getText());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            s.setBirthyear(format.parse(textField4.getText()));
            s.setGrade(format.parse(textField5.getText()));
            s.setCollege(textField6.getText());
            s.setOffice(textField7.getText());
            int flag=addTeacher(s);
            if(flag!=0){
                JOptionPane.showMessageDialog(this, "添加成功！");
                this.dispose();
            }
            else{JOptionPane.showMessageDialog(this, "添加失败！");}
        }
        catch (ParseException e1) {
            e1.printStackTrace();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label10 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        textField5 = new JTextField();
        textField6 = new JTextField();
        textField7 = new JTextField();
        button1 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label3 ----
        label3.setText("\u59d3\u540d");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 5f));
        contentPane.add(label3);
        label3.setBounds(75, 70, 80, 30);

        //---- label4 ----
        label4.setText("\u6027\u522b");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 5f));
        contentPane.add(label4);
        label4.setBounds(75, 120, 80, 30);

        //---- label5 ----
        label5.setText("\u51fa\u751f\u65e5\u671f");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 5f));
        contentPane.add(label5);
        label5.setBounds(75, 170, 80, 30);

        //---- label6 ----
        label6.setText("\u5165\u804c\u65e5\u671f");
        label6.setFont(label6.getFont().deriveFont(label6.getFont().getSize() + 5f));
        contentPane.add(label6);
        label6.setBounds(75, 220, 80, 30);

        //---- label7 ----
        label7.setText("\u5b66\u9662");
        label7.setFont(label7.getFont().deriveFont(label7.getFont().getSize() + 5f));
        contentPane.add(label7);
        label7.setBounds(75, 270, 80, 30);

        //---- label8 ----
        label8.setText("\u6559\u7814\u5ba4");
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 5f));
        contentPane.add(label8);
        label8.setBounds(75, 320, 80, 30);

        //---- label10 ----
        label10.setText("\u5de5\u53f7");
        label10.setFont(label10.getFont().deriveFont(label10.getFont().getSize() + 5f));
        contentPane.add(label10);
        label10.setBounds(75, 20, 80, 30);
        contentPane.add(textField1);
        textField1.setBounds(170, 20, 150, textField1.getPreferredSize().height);
        contentPane.add(textField2);
        textField2.setBounds(170, 70, 150, textField2.getPreferredSize().height);
        contentPane.add(textField3);
        textField3.setBounds(170, 120, 150, textField3.getPreferredSize().height);
        contentPane.add(textField4);
        textField4.setBounds(170, 170, 150, textField4.getPreferredSize().height);
        contentPane.add(textField5);
        textField5.setBounds(170, 220, 150, textField5.getPreferredSize().height);
        contentPane.add(textField6);
        textField6.setBounds(170, 270, 150, textField6.getPreferredSize().height);
        contentPane.add(textField7);
        textField7.setBounds(170, 320, 150, textField7.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u6dfb\u52a0");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 5f));
        button1.addActionListener(e -> addteacher(e));
        contentPane.add(button1);
        button1.setBounds(160, 425, 85, 45);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label10;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JTextField textField7;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
